/*
 * Author: Fernando RUIZ CASAS
 *
 *  Work: fernando.ruiz@ctv.es
 *  Home: correo@fernando-ruiz.com
 *
 * This file is inspired in rtems_monitor & Chris John MyRightBoot
 * 
 * But I want to make it more user friendly
 * A 'monitor' command is added to adapt the call rtems monitor commands
 * at my call procedure
 * 
 * TODO: A lot of improvements of course. 
 *      cp, mv, ...
 *      hexdump,
 *      
 *      More? Say me it, please...
 *      
 *      The BSP Specific are not welcome here.
 *      
 * C&S welcome...
 *
 *  $Id: cmds.c,v 1.5.6.2 2003/08/06 21:54:56 jennifer Exp $
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <stddef.h>

#include "shell.h"

/* ----------------------------------------------- *
  - str to int "0xaffe" "0b010010" "0123" "192939"
 * ----------------------------------------------- */
uint64_t str2int(char * s) {
 int64_t sign=1;
 int64_t base=10;
 int64_t value=0;
 int64_t digit;
 if (!s) return 0;
 if (*s) {
  if (*s=='-') {
   sign=-1;
   s++;
   if (!*s) return 0;
  };
  if (*s=='0') {
   s++;
   switch(*s) {
    case 'x':
    case 'X':s++;
 	     base=16;
	     break;
    case 'b':
    case 'B':s++;
	     base=2;
	     break;
    default :base=8;
	     break;
   }
  }; 
  while (*s) {
   switch(*s) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':digit=*s-'0';
 	     break;
    case 'A':	   
    case 'B':	   
    case 'C':	   
    case 'D':	   
    case 'E':	   
    case 'F':digit=*s-'A'+10;
	     break;
    case 'a':	   
    case 'b':	   
    case 'c':	   
    case 'd':	   
    case 'e':	   
    case 'f':digit=*s-'a'+10;
	     break;
    default:return value*sign;	     
   };
   if (digit>base) return value*sign;
   value=value*base+digit;
   s++;
  };
 };
 return value*sign;	
}

/*-----------------------------------------------------------*/  	
void register_cmds(void) {

    
}
/*-----------------------------------------------------------*/  	
// max getline len
#define MAX_COUNT 256
void process_line(unsigned char* line)
{
    char cmd_tok[MAX_COUNT];
    int argc;
    char* argv[128];
    shell_cmd_t* shell_cmd;

    strcpy(cmd_tok, (char *) line);
    shell_make_args(cmd_tok, &argc, argv);

    if ((shell_cmd = shell_lookup_cmd(argv[0])) != NULL)
    {
        if (shell_cmd->command != NULL)
        {
            shell_cmd->command(argc, argv);
        }
        return;
    }

    printf("invalid command %s, check help\n", line);
}
